<template>
  <div class="app-container home">
    <h1>牛马招聘，欢迎您！</h1>
    <h2>牛会哞 马会叫 牛马会收到</h2>
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="工作名称" prop="jobName">
        <el-input
            v-model="queryParams.jobName"
            placeholder="请输入工作名称"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="jobList" style="width: 100%">
      <el-table-column prop="jobName" label="职位名称" width="180"></el-table-column>
      <el-table-column prop="positionName" label="职位类型" width="180"></el-table-column>
      <el-table-column prop="jobAsk" label="职位要求" width="180"></el-table-column>
      <el-table-column prop="jobBudget" label="薪资预算" width="180"></el-table-column>
      <el-table-column label="工作地点" align="center">
        <template #default="scope">
          {{ getCityName(scope.row.address) }}
        </template>
      </el-table-column>      <!--      <el-table-column prop="companyId" label="公司ID" width="180"></el-table-column>-->
      <el-table-column prop="jobDescription" label="职位描述" width="180"></el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)">
            投递
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="jobRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="工作名称" prop="jobName" >
          <el-input v-model="form.jobName" placeholder="请输入工作名称" :disabled="isDisabled"/>
        </el-form-item>
        <el-form-item label="薪资" prop="jobBudget">
          <el-input v-model="form.jobBudget" placeholder="请输入薪资范围" :disabled="isDisabled"/>
        </el-form-item>
        <el-form-item label="工作地点" prop="address">
          <el-select v-model="form.address" placeholder="请选择工作地点" :disabled="isDisabled">
            <el-option v-for="city in cityOptions" :key="city.value" :label="city.label"
                       :value="city.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="职位类型" prop="jobType">
          <el-input v-model="form.jobType" placeholder="请输入职位类型" :disabled="isDisabled"/>
        </el-form-item>
        <el-form-item label="简介" prop="positionName">
          <el-input v-model="form.positionName" type="textarea" placeholder="请输入简介" :disabled="isDisabled"/>
        </el-form-item>
        <el-form-item label="工作要求" prop="jobAsk">
          <el-input v-model="form.jobAsk" type="textarea" placeholder="请输入工作要求" :disabled="isDisabled"/>
        </el-form-item>
        <el-form-item label="工作描述" prop="jobDescription">
          <el-input v-model="form.jobDescription" type="textarea" placeholder="请输入工作描述" :disabled="isDisabled"/>
        </el-form-item>
        <el-form-item label="简历列表">
          <el-table :data="resumeList" style="width: 100%">
            <el-table-column prop="resumeName" label="简历名称" width="180"></el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
              <template #default="scope">
                <el-button link type="primary" icon="Edit" v-hasPermi="['nmzp:job:deliver']" @click="handleDeliver(scope.row)">
                  投递
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
<!--          <el-button type="primary" @click="handleDeliver(form)">确 认</el-button>-->
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>

    </el-dialog>
  </div>
</template>

<script setup>
import {ref, onMounted} from 'vue';
import {ElMessage} from 'element-plus';
import {jobLists, listJob, jobListSerch, getJob, examineJob, examineDeliver} from "@/api/nmzp/job";
import {listResume} from "@/api/nmzp/resume";


const {proxy} = getCurrentInstance();
const cityOptions = [
  {label: "北京", value: "1"},
  {label: "上海", value: "2"},
  {label: "天津", value: "3"},
  {label: "重庆", value: "4"},
  {label: "广州", value: "5"},
  {label: "深圳", value: "6"},
  {label: "成都", value: "7"},
  {label: "武汉", value: "8"},
  {label: "南京", value: "9"},
  {label: "西安", value: "10"},
  {label: "杭州", value: "11"},
  {label: "青岛", value: "12"},
  {label: "大连", value: "13"},
  {label: "宁波", value: "14"},
  {label: "厦门", value: "15"},
  {label: "苏州", value: "16"},
  {label: "佛山", value: "17"},
  {label: "长沙", value: "18"},
  {label: "郑州", value: "19"},
  {label: "济南", value: "20"},
  {label: "哈尔滨", value: "21"},
  {label: "长春", value: "22"},
  {label: "沈阳", value: "23"},
  {label: "昆明", value: "24"},
  {label: "南宁", value: "25"},
  {label: "海口", value: "26"},
  {label: "兰州", value: "27"},
  {label: "乌鲁木齐", value: "28"},
  {label: "呼和浩特", value: "29"},
  {label: "石家庄", value: "30"},
  {label: "太原", value: "31"},
  {label: "合肥", value: "32"},
  {label: "南昌", value: "33"},
  {label: "福州", value: "34"},
  {label: "贵阳", value: "35"},
  {label: "西宁", value: "36"},
  {label: "拉萨", value: "37"},
  {label: "银川", value: "38"},
  {label: "澳门", value: "39"},
  {label: "香港", value: "40"}
];
const cityMap = {
  "1": "北京",
  "2": "上海",
  "3": "天津",
  "4": "重庆",
  "5": "广州",
  "6": "深圳",
  "7": "成都",
  "8": "武汉",
  "9": "南京",
  "10": "西安",
  "11": "杭州",
  "12": "青岛",
  "13": "大连",
  "14": "宁波",
  "15": "厦门",
  "16": "苏州",
  "17": "佛山",
  "18": "长沙",
  "19": "郑州",
  "20": "济南",
  "21": "哈尔滨",
  "22": "长春",
  "23": "沈阳",
  "24": "昆明",
  "25": "南宁",
  "26": "海口",
  "27": "兰州",
  "28": "乌鲁木齐",
  "29": "呼和浩特",
  "30": "石家庄",
  "31": "太原",
  "32": "合肥",
  "33": "南昌",
  "34": "福州",
  "35": "贵阳",
  "36": "西宁",
  "37": "拉萨",
  "38": "银川",
  "39": "澳门",
  "40": "香港"
};
const title = ref("");
const open = ref(false);
const isDisabled=ref(true)
const resumeList = ref([]);
const total = ref(0);

const jobList = ref([]);
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    jobName: null,
    positionName: null,
    jobAsk: null,
    jobBudget: null,
    address: null,
    companyId: null,
    publishUserId: null,
    jobDescription: null,
    createUser: null,
    jobType: null,
    resumeId: null,
    idDel: null
  },
  rules: {},

});

// 表单重置
function reset() {
  form.value = {
    jobId: null,
    jobName: null,
    positionName: null,
    jobAsk: null,
    jobBudget: null,
    address: null,
    companyId: null,
    publishUserId: null,
    jobDescription: null,
    createTime: null,
    updateTime: null,
    createUser: null,
    jobType: null,
    resumeId: null,
    idDel: null,
    status: null,
  };
  resumeList.value=[];
  proxy.resetForm("jobRef");
}

const showSearch = ref(true);
const {queryParams, form, rules} = toRefs(data);
onMounted(() => {
  fetchJobList();
});

async function fetchJobList() {
  try {
    const response = await jobLists();
    jobList.value = response.rows;
  } catch (error) {
    ElMessage.error('请求出错');
  }
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 查询职位管理列表 */
function getList() {
  jobListSerch(queryParams.value).then(response => {
    jobList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });

}
function getCityName(code) {
  return this.cityMap[code] || code;
}
const jobIds = ref(null);
/**
 * 投递按钮
 * @param row
 */
function handleUpdate(row) {
  reset();
  const _jobId = row.jobId || ids.value
  getJob(_jobId).then(response => {
    form.value = response.data;
    form.value.address = cityMap[response.data.address];
    open.value = true;
    title.value = "简历投递";
    jobIds.value =_jobId;
  });
  listResume(queryParams.value).then(response => {
    resumeList.value = response.rows;
    total.value = response.total;
    // loading.value = false;
  });
}

/**
 * 岗位投递
 */
function  handleDeliver(row){
  console.log(row)
  const requestData = {
    jobId: jobIds.value,
    resumeId: row.resumeId,
  };
  examineDeliver(requestData).then(response => {
    proxy.$modal.msgSuccess(response.msg);
    open.value = false;
    getList();
  });

}
function cancel() {
  open.value = false;
  reset();


}

</script>

<style scoped lang="scss">
.app-container {
  padding: 20px;
}

h1 {
  font-size: 2em;
  margin-bottom: 0.5em;
}

h2 {
  font-size: 1.5em;
  color: #666;
  margin-bottom: 1em;
}
</style>
